<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>程序自动分配开关</title>
		<script src="js/global.js"></script>
	</head>

	<body>

		<div id="kaiguanchuang" style="display: none">
			<form class="layui-form" lay-filter="kaiguanForm">
				<!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
				<div class="layui-form-item">
					<h2>程序自动分配开关</h2>
					<div class="layui-input-block">
						<input type='checkbox' name='isAutoconfig' lay-filter='kaiguan' lay-skin='switch' lay-text='开|关' />
					</div>
				</div>
			</form>
		</div>
		
		<script type="text/javascript">
			layui.use(['form', 'table', 'layer'], function() {
				var form = layui.form;
				var table = layui.table;
				var layer = layui.layer;
				var $ = layui.$;


				//获取开关初始值
				$.post(globalData.server + "autoconfig/getConfigAuto",{
					"token": globalData.getToken()
				},function(res){
					if(res.code==0){
						console.log(res.data);
						if(res.data.isAutoconfig==0){
							//表单赋值
							form.val("kaiguanForm",{
								"isAutoconfig":false
							})
						}else{
							form.val("kaiguanForm",{
								"isAutoconfig":true
							})
						}
					}
				})//获取开关初始值--end



				//开关监听事件
				form.on('switch(kaiguan)', function(data) {
					console.log(data.elem.checked); //是否被选中，true或者false
					console.log(data.value); //复选框value值，也可以通过data.elem.value得到
					var url = "";
					if(data.elem.checked == true) {
						url = globalData.server + "autoconfig/setConfigAutoOn";
					} else {
						url = globalData.server + "autoconfig/setConfigAutoOff";
					}
					$.post(url, {
						"token": globalData.getToken()
					}, function(res) {
						console.log(res);
						if(res.code == 0) {
							layer.msg(res.msg, {
								icon: 1,
								time:1000
							}, function() {
								layer.closeAll();
								window.location.reload(); //刷新页面
							});
						}
					})
				});//开关监听事件--end

				layer.open({
					title:"你正在使用自动分配开关",
					type: 1,
					area: ['500px', '400px'],
					content: $("#kaiguanchuang")
				})
			});
		</script>
	</body>

</html>